Python

Data extraction

import pandas as pd
import numpy as np
import shapefile as shp
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.figure_factory as ff
import geopandas as gpd
from geopandas import GeoDataFrame
pres = pd.read_csv('countypres_2000-2016.csv')
pres = pres.loc[pres['year'].isin([2008, 2012, 2016])]
pres = pres.loc[pres['party'] == 'republican']
pres['win'] = pres['candidatevotes']/pres['totalvotes']*100
pres['FIPS'] = pres['FIPS'].apply(str)
pres['FIPS'] = pres['FIPS'].str[:-2]
pres['FIPS'] = pres['FIPS'].str.zfill(5)
pres = pres.loc[pres['state_po'].isin(['TX', 'CA', 'PA'])]

pres_pivot = pres.pivot_table(index = ['FIPS'],columns = 'year', values = 'win')
pres_pivot.columns = ['win_2008','win_2012','win_2016']
pres_pivot['FIPS'] = pres_pivot.index
pres_pivot = pres_pivot.reset_index(drop=True)


unemp = pd.read_excel('Unemployment.xls', sheet_name = 0, header = 7)
unemp = unemp[['FIPStxt','Stabr','area_name','Unemployment_rate_2008',
'Unemployment_rate_2012','Unemployment_rate_2016']]
unemp.rename(columns={'FIPStxt':'FIPS'}, inplace=True)
unemp['FIPS'] = unemp['FIPS'].apply(str)
unemp['FIPS'] = unemp['FIPS'].str.zfill(5)
unemp[['county','state_po']] = unemp.area_name.str.split("County, ",expand=True) 
unemp = unemp.drop(['area_name','Stabr'], axis=1)
unemp = unemp.loc[unemp['state_po'].isin(['TX', 'CA', 'PA'])]

sf = gpd.read_file('tl_2019_us_county.shp')

df = pd.merge(unemp, sf, left_on='FIPS', right_on='GEOID')
df = pd.merge(df, pres_pivot, on=['FIPS'])

plotdf = GeoDataFrame(df)
CA_plot = plotdf.loc[plotdf['state_po']=='CA']
TX_plot = plotdf.loc[plotdf['state_po']=='TX']
PA_plot = plotdf.loc[plotdf['state_po']=='PA']

Visualization

fig = plt.figure(figsize=(15,15))
annot = 'The red plots on the left side show the percentage of votes Republican Candidate won in each county during the election.\nThe blue plots on the right side represent the unemployment rate in each county. \nAll numbers are in percentages.\n'

plt.suptitle('California', fontsize=20, y=1)
plt.title(annot, fontsize=10)
plt.axis('off')
## (0.0, 1.0, 0.0, 1.0)
ax1 = fig.add_subplot(322)
CA_plot.plot(column='Unemployment_rate_2008', ax=ax1,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax1.axis('off')
## (-124.99954259999998, -113.61367139999997, 32.05479845, 42.48353655)
ax1.set_title('Unemployment Rate in 2008', fontsize=15)
plt.axis("equal")
## (-124.99954259999998, -113.61367139999997, 32.05479845, 42.48353655)
ax2 = fig.add_subplot(324)
CA_plot.plot(column='Unemployment_rate_2012', ax=ax2,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax2.axis('off')
## (-124.99954259999998, -113.61367139999997, 32.05479845, 42.48353655)
ax2.set_title('Unemployment Rate in 2012', fontsize=15)
plt.axis("equal")
## (-124.99954259999998, -113.61367139999997, 32.05479845, 42.48353655)
ax3 = fig.add_subplot(326)
CA_plot.plot(column='Unemployment_rate_2016', ax=ax3,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax3.axis('off')
## (-124.99954259999998, -113.61367139999997, 32.05479844999999, 42.48353654999999)
ax3.set_title('Unemployment Rate in 2016', fontsize=15)
plt.axis("equal")
## (-124.99954259999998, -113.61367139999997, 32.05479844999999, 42.48353654999999)
ax4 = fig.add_subplot(321)
CA_plot.plot(column='win_2008', ax=ax4, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax4.axis('off')
## (-124.9995426, -113.61367139999999, 32.05479845, 42.48353655)
ax4.set_title("Repuclican's Electoral Map in 2008", fontsize=15)
plt.axis("equal")
## (-124.9995426, -113.61367139999999, 32.05479845, 42.48353655)
ax5 = fig.add_subplot(323)
CA_plot.plot(column='win_2012', ax=ax5, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax5.axis('off')
## (-124.9995426, -113.61367139999999, 32.05479845, 42.48353655)
ax5.set_title("Repuclican's Electoral Map in 2012", fontsize=15)
plt.axis("equal")
## (-124.9995426, -113.61367139999999, 32.05479845, 42.48353655)
ax6 = fig.add_subplot(325)
CA_plot.plot(column='win_2016', ax=ax6, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax6.axis('off')
## (-124.9995426, -113.61367139999999, 32.05479844999999, 42.48353654999999)
ax6.set_title("Repuclican's Electoral Map in 2016", fontsize=15)
plt.axis("equal")
## (-124.9995426, -113.61367139999999, 32.05479844999999, 42.48353654999999)
fig.tight_layout()
plt.show()

fig = plt.figure(figsize=(15,15))
plt.suptitle('Texas', fontsize=20, y=1)
plt.title(annot, fontsize=10)
plt.axis('off')
## (0.0, 1.0, 0.0, 1.0)
ax1 = fig.add_subplot(322)
TX_plot.plot(column='Unemployment_rate_2008', ax=ax1,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax1.axis('off')
## (-107.30252634999997, -92.85115864999999, 25.303987, 37.033881)
ax1.set_title('Unemployment Rate in 2008', fontsize=15)
plt.axis("equal")
## (-107.30252634999997, -92.85115864999999, 25.303987, 37.033881)
ax2 = fig.add_subplot(324)
TX_plot.plot(column='Unemployment_rate_2012', ax=ax2,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax2.axis('off')
## (-107.30252634999997, -92.85115864999999, 25.303986999999992, 37.033881)
ax2.set_title('Unemployment Rate in 2012', fontsize=15)
plt.axis("equal")
## (-107.30252634999997, -92.85115864999999, 25.303986999999992, 37.033881)
ax3 = fig.add_subplot(326)
TX_plot.plot(column='Unemployment_rate_2016', ax=ax3,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax3.axis('off')
## (-107.30252634999997, -92.85115864999999, 25.303987, 37.033880999999994)
ax3.set_title('Unemployment Rate in 2016', fontsize=15)
plt.axis("equal")
## (-107.30252634999997, -92.85115864999999, 25.303987, 37.033880999999994)
ax4 = fig.add_subplot(321)
TX_plot.plot(column='win_2008', ax=ax4, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax4.axis('off')
## (-107.30252635, -92.85115865, 25.303987, 37.033881)
ax4.set_title("Repuclican's Electoral Map in 2008", fontsize=15)
plt.axis("equal")
## (-107.30252635, -92.85115865, 25.303987, 37.033881)
ax5 = fig.add_subplot(323)
TX_plot.plot(column='win_2012', ax=ax5, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax5.axis('off')
## (-107.30252635, -92.85115865, 25.303986999999992, 37.033881)
ax5.set_title("Repuclican's Electoral Map in 2012", fontsize=15)
plt.axis("equal")
## (-107.30252635, -92.85115865, 25.303986999999992, 37.033881)
ax6 = fig.add_subplot(325)
TX_plot.plot(column='win_2016', ax=ax6, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax6.axis('off')
## (-107.30252635, -92.85115865, 25.303987, 37.033880999999994)
ax6.set_title("Repuclican's Electoral Map in 2016", fontsize=15)
plt.axis("equal")
## (-107.30252635, -92.85115865, 25.303987, 37.033880999999994)
fig.tight_layout()
plt.show()

fig = plt.figure(figsize=(15,15))
plt.suptitle('Pennsylvania', fontsize=20, y=1)
plt.title(annot, fontsize=10)
plt.axis('off')
## (0.0, 1.0, 0.0, 1.0)
ax1 = fig.add_subplot(322)
PA_plot.plot(column='Unemployment_rate_2008', ax=ax1,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax1.axis('off')
## (-80.81136844999999, -74.39798454999998, 39.579985349999994, 42.65588565)
ax1.set_title('Unemployment Rate in 2008', fontsize=15)
plt.axis("equal")
## (-80.81136844999999, -74.39798454999998, 39.579985349999994, 42.65588565)
ax2 = fig.add_subplot(324)
PA_plot.plot(column='Unemployment_rate_2012', ax=ax2,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax2.axis('off')
## (-80.81136844999999, -74.39798454999998, 39.579985349999994, 42.65588565)
ax2.set_title('Unemployment Rate in 2012', fontsize=15)
plt.axis("equal")
## (-80.81136844999999, -74.39798454999998, 39.579985349999994, 42.65588565)
ax3 = fig.add_subplot(326)
PA_plot.plot(column='Unemployment_rate_2016', ax=ax3,cmap='Blues',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax3.axis('off')
## (-80.81136844999999, -74.39798454999998, 39.579985349999994, 42.655885649999995)
ax3.set_title('Unemployment Rate in 2016', fontsize=15)
plt.axis("equal")
## (-80.81136844999999, -74.39798454999998, 39.579985349999994, 42.655885649999995)
ax4 = fig.add_subplot(321)
PA_plot.plot(column='win_2008', ax=ax4, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax4.axis('off')
## (-80.81136845, -74.39798454999999, 39.579985349999994, 42.65588565)
ax4.set_title("Repuclican's Electoral Map in 2008", fontsize=15)
plt.axis("equal")
## (-80.81136845, -74.39798454999999, 39.579985349999994, 42.65588565)
ax5 = fig.add_subplot(323)
PA_plot.plot(column='win_2012', ax=ax5, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax5.axis('off')
## (-80.81136845, -74.39798454999999, 39.579985349999994, 42.65588565)
ax5.set_title("Repuclican's Electoral Map in 2012", fontsize=15)
plt.axis("equal")
## (-80.81136845, -74.39798454999999, 39.579985349999994, 42.65588565)
ax6 = fig.add_subplot(325)
PA_plot.plot(column='win_2016', ax=ax6, cmap='Reds',
                legend=True,linewidth=0.8, edgecolor='0.8')
ax6.axis('off')
## (-80.81136845, -74.39798454999999, 39.579985349999994, 42.655885649999995)
ax6.set_title("Repuclican's Electoral Map in 2016", fontsize=15)
plt.axis("equal")
## (-80.81136845, -74.39798454999999, 39.579985349999994, 42.655885649999995)
fig.tight_layout()
plt.show()

R

Data extraction

library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(reshape)
## 
## Attaching package: 'reshape'
## The following object is masked from 'package:dplyr':
## 
##     rename
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.4-4, (SVN revision 833)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/gdal
##  GDAL binary built with GEOS: FALSE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/proj
##  Linking to sp version: 1.3-1
library(maptools)
## Checking rgeos availability: TRUE
library(ggplot2)
library(plyr)
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:reshape':
## 
##     rename, round_any
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
# read datasets
unemp <- read_excel("Unemployment.xls",sheet=1, skip=7, col_names = TRUE)
unemp = unemp[,c('FIPStxt','Stabr','Unemployment_rate_2008', 'Unemployment_rate_2012','Unemployment_rate_2016')]
states <- c('CA','TX','PA')
unemp <- filter(unemp, Stabr %in% states)
pres <- read.csv('countypres_2000-2016.csv', header = TRUE)
pres <- pres[which(pres['party']=='republican'),]
years = c(2008,2012,2016)
pres <- filter(pres, year %in% years)
pres <- filter(pres, state_po %in% states)
pres['win'] <- pres['candidatevotes']/pres['totalvotes']*100
pres <- pres[,c('year','county','FIPS','win')]
pres$FIPS <- sprintf('%05d', pres$FIPS)
pres <- cast(pres, FIPS + county ~ year)
## Using win as value column.  Use the value argument to cast to override this choice
colnames(pres) <- c('FIPS', 'county','win_2008','win_2012','win_2016')

df <- merge(pres, unemp, by.x='FIPS', by.y='FIPStxt', all.x=TRUE)
# read shapefile
esp<- readOGR(dsn=".", layer="tl_2019_us_county")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/wangmiao/Downloads/anly503-fall2020-a6-MiaoWang1009-main/data", layer: "tl_2019_us_county"
## with 3233 features
## It has 17 fields
## Integer64 fields read as strings:  ALAND AWATER
muni <- subset(esp, esp$STATEFP == "06" | esp$STATEFP == "42" | esp$STATEFP == "48")
# fortify and merge: muni.df is used in ggplot
muni@data$id <- rownames(muni@data)
muni.df <- fortify(muni)
## Regions defined for each Polygons
muni.df <- join(muni.df, muni@data, by="id")
muni.df <- merge(muni.df, df, by.x="GEOID", by.y="FIPS", all.x=T, a..ly=F)
#head(muni.df)
CAplot <- muni.df[which(muni.df$Stabr=='CA'),]
TXplot <- muni.df[which(muni.df$Stabr=='TX'),]
PAplot <- muni.df[which(muni.df$Stabr=='PA'),]

annot = 'The red plots on the left side show the percentage of votes Republican Candidate won in each county during the election.\nThe blue plots on the right side represent the unemployment rate in each county. \nAll numbers are in percentages.\n'

Visualization

# create the map layers
ggp01 <- ggplot(data=CAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2008)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(CAplot$win_2008), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2008") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp03 <- ggplot(data=CAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2012)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(CAplot$win_2012), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2012") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp05 <- ggplot(data=CAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2016)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(CAplot$win_2016), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2016") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp02 <- ggplot(data=CAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2008)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") +
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Unemployment Rate in 2008") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp04 <- ggplot(data=CAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2012)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") +
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Unemployment Rate in 2012") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp06 <- ggplot(data=CAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2016)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) + 
  labs(title="Unemployment Rate in 2016") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))
# create the map layers
ggp001 <- ggplot(data=TXplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2008)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(TXplot$win_2008), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2016") +
  labs(title="Repuclican's Electoral Map in 2008") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp003 <- ggplot(data=TXplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2012)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(TXplot$win_2012), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2012") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp005 <- ggplot(data=TXplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2016)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(TXplot$win_2016), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2016") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp002 <- ggplot(data=TXplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2008)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) + 
  labs(title="Unemployment Rate in 2008") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp004 <- ggplot(data=TXplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2012)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) + 
  labs(title="Unemployment Rate in 2012") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp006 <- ggplot(data=TXplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2016)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) + 
  labs(title="Unemployment Rate in 2016") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))
# create the map layers
ggp0001 <- ggplot(data=PAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2008)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(PAplot$win_2008), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2008") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp0003 <- ggplot(data=PAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2012)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(PAplot$win_2012), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2012") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp0005 <- ggplot(data=PAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=win_2016)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient2(midpoint = mean(PAplot$win_2016), low = "#0BA925", mid ="#FFFFFF", 
                       high = "#045185", space = "Lab", na.value = "grey50", guide = "colourbar") + 
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Repuclican's Electoral Map in 2016") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp0002 <- ggplot(data=PAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2008)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") +
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Unemployment Rate in 2008") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp0004 <- ggplot(data=PAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2012)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") +
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Unemployment Rate in 2012") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))

ggp0006 <- ggplot(data=PAplot, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=Unemployment_rate_2016)) + # draw polygons
  geom_path(color="grey", size=0.1) +  # draw boundaries
  coord_equal() + 
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", 
                      na.value = "grey50", guide = "colourbar") +
  guides(fill = guide_colorbar(barheight = 4)) +
  labs(title="Unemployment Rate in 2016") +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  theme(panel.background = element_blank(), legend.title = element_blank(),
        axis.title.x = element_blank(), axis.title.y = element_blank(),
        text = element_text(size = 10), element_line(size = 0.4))
grid.arrange(ggp01, ggp02, ggp03, ggp04, ggp05, ggp06, nrow=3, ncol=2, 
             top = grid::textGrob('California', gp = grid::gpar(fontsize = 15)), 
             bottom = grid::textGrob(annot, gp = grid::gpar(fontsize = 8), x=0.5))

grid.arrange(ggp001, ggp002, ggp003, ggp004, ggp005, ggp006, nrow=3, ncol=2, 
             top = grid::textGrob('Texas', gp = grid::gpar(fontsize = 15)), 
             bottom = grid::textGrob(annot, gp = grid::gpar(fontsize = 8), x=0.5))

grid.arrange(ggp0001, ggp0002, ggp0003, ggp0004, ggp0005, ggp0006, nrow=3, ncol=2, 
             top = grid::textGrob('Pennsylvania', gp = grid::gpar(fontsize = 15)), 
             bottom = grid::textGrob(annot, gp = grid::gpar(fontsize = 8), x=0.5))